package com.chiaching.oms.component;

import com.chiaching.oms.entity.OrderBaseEntity;
import com.chiaching.oms.service.OrderBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @email 1738870995@qq.com
 * @author: 何佳靖
 * @date: 2021/10/20 22:47
 */
@Component
@Slf4j
public class OrderBaseDataInit {

    @Resource
    private OrderBaseService orderBaseService;

    private AtomicLong atomicLong = new AtomicLong(System.currentTimeMillis() / 1000);

    private AtomicLong globalOrderTraceId = new AtomicLong(System.currentTimeMillis() / 888 + 123456);

    private final List<String> orderFrom =  Arrays.asList("site","ikea","weixin" ,"thirdpart" ,"ios" ,"android" ,"applet");

    private final List<String> orderStatus = Arrays.asList("wait_offer","wait_appoint","wait_pay","wait_serve_complete","serve_complete","finish","stop");

    private final List<String> appointMethod = Arrays.asList("normal","open","definite_price","advance_payment");

    private final List<String> spType = Arrays.asList("unknown","master", "enterprise");

    private final List<String> orderType = Arrays.asList("inner_normal","inner_monthly");

    private final List<String> refundStatus = Arrays.asList("refund_auditing","arbitration_auditing","pass","refuse","close");

    private final List<String> orderPayStatus = Arrays.asList("unpaid","paid","delay");

    private final List<String> orderChangeType = Arrays.asList("common","change","adjust");

    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @PostConstruct
    public void init(){
        for (int i = 0; i < 3; i++) {
            threadPoolTaskExecutor.execute(() ->{
                initOrderBase();
                return;
            });
        }

    }
    private void initOrderBase() {
        Random random=new Random();
        while (true){
            try {
                OrderBaseEntity orderBaseEntity = new OrderBaseEntity();
                orderBaseEntity.setOrderFrom(orderFrom.get(random.nextInt(orderFrom.size())));
                orderBaseEntity.setOrderNo("P"+atomicLong.getAndIncrement());
                orderBaseEntity.setCategoryId(random.nextInt(100));
                orderBaseEntity.setServeTypeId(random.nextInt(100));
                orderBaseEntity.setServeType(random.nextInt(8));
                orderBaseEntity.setBusinessLineId(Integer.valueOf(random.nextInt(4)).longValue());
                orderBaseEntity.setServeLevel1Id(Integer.valueOf(random.nextInt(21)).longValue());
                ArrayList<String> serveIds = new ArrayList();
                for (int i = 0; i < random.nextInt(6); i++) {
                    serveIds.add(random.nextInt(31)+"");
                }
                orderBaseEntity.setServeIds(String.join(",", serveIds));
                orderBaseEntity.setUserId(Integer.valueOf(random.nextInt(999999)+167839).longValue());
                orderBaseEntity.setSubUserId(Integer.valueOf(random.nextInt(999999)+167839).longValue());
                orderBaseEntity.setOrderStatus(orderStatus.get(random.nextInt(orderStatus.size())));
                orderBaseEntity.setRegionId(random.nextInt(999)+10000);
                orderBaseEntity.setThirdDivisionId(Integer.valueOf(random.nextInt(999)+100000).longValue());
                orderBaseEntity.setFourthDivisionId(Integer.valueOf(random.nextInt(999)+1000000).longValue());
                orderBaseEntity.setAppointMethod(appointMethod.get(random.nextInt(appointMethod.size())));
                orderBaseEntity.setSpType(spType.get(random.nextInt(spType.size())));
                orderBaseEntity.setOrderType(orderType.get(random.nextInt(orderType.size())));
                orderBaseEntity.setIsDelete(random.nextInt(2));
                orderBaseEntity.setRefundStatus(refundStatus.get(random.nextInt(refundStatus.size())));
                orderBaseEntity.setOrderPayStatus(orderPayStatus.get(random.nextInt(orderPayStatus.size())));
                orderBaseEntity.setServeWorkCompleteTime(new Date());
                orderBaseEntity.setTradeCompleteTime(new Date());
                orderBaseEntity.setOrderLabel(random.nextInt(5)>2?"rigorous_selection":"");
                orderBaseEntity.setGlobalOrderTraceId(globalOrderTraceId.getAndIncrement());
                orderBaseEntity.setCreateTime(new Date());
                orderBaseEntity.setUpdateTime(new Date());
                orderBaseEntity.setOrderChangeType(orderChangeType.get(random.nextInt(orderChangeType.size())));
                orderBaseService.save(orderBaseEntity);
                atomicInteger.incrementAndGet();
                if (atomicInteger.compareAndSet(10000, 0)){
                    log.info("10000个订单生成成功.........[{}]",atomicLong.get());
                }
            } catch (Exception e) {
               log.error("生成订单异常", e);
            }
        }
    }


}
